package freespiritsocial.io.user.ui.fragment.mine

import android.os.Bundle
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.databinding.FrgFollowsBinding
import freespiritsocial.io.user.ui.fragment.mine.MineFrg.Companion.KEY_IS_Follow_Page
import freespiritsocial.io.user.util.CacheUtil
import freespiritsocial.io.user.viewmodel.FollowsViewModel
import me.hgj.jetpackmvvm.ext.nav

/**
 * @description 粉丝/关注
 */
class FollowsFrg: BaseFragment<FollowsViewModel,FrgFollowsBinding>() {

    override fun layoutId(): Int = R.layout.frg_follows

    val user by lazy(LazyThreadSafetyMode.NONE) {
        CacheUtil.getUser()
    }

    override fun initView(savedInstanceState: Bundle?) = with(mDatabind) {
        toolbar.setNavigationOnClickListener {
            nav().navigateUp()
        }
        viewpagerFollow.adapter = InnerPagerAdapter(childFragmentManager)
        tabFollow.setViewPager(viewpagerFollow,arrayOf("${user?.attentionCount} Following","${user?.fansCount} Followers"))
        tabFollow.setCurrentTab(arguments?.getInt(KEY_IS_Follow_Page,0) ?: 0,false)
    }

    inner class Click {

    }


    internal class InnerPagerAdapter(fm: FragmentManager):
        FragmentStatePagerAdapter(fm,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
        private val fragments = arrayListOf<Fragment>(FollowsListFrg.crate(true),FollowsListFrg.crate(false))

        //        private val titles: Array<String>
        override fun getCount(): Int {
            return fragments.size
        }

        override fun getItem(position: Int): Fragment {
            return fragments[position]
        }

        override fun destroyItem(container: ViewGroup,position: Int,`object`: Any) {
            // 覆写destroyItem并且空实现,这样每个Fragment中的视图就不会被销毁
            // super.destroyItem(container, position, object);
        }

        override fun getItemPosition(`object`: Any): Int {
            return POSITION_NONE
        }
    }
}